import {Vote} from "../../models/vote";
const mixins = require('../../common/mixins/index')

Page({
    data: {
        personPage: 1,
        projectPage: 1,
        personHasMore: true,
        projectHasMore: true,
        isTop: true,
        active: 'person',
        personList: [],
        projectList: [],
    },

    async onLoad(options) {
        await this.getData('person')
        await this.getData('project')
    },

    async getData(type) {
        const res = await Vote.getVoteList(type, this.data[`${type}Page`], this.data[`${type}List`].length)
        const list = this.data[`${type}List`].concat(res.list)
        this.data[`${type}HasMore`] = res.hasMore
        this.setData({
            [`${type}List`]: list
        })
    },

    async onReachBottom() {
        const activeType = this.data.active
        if(!this.data[`${activeType}HasMore`]) return
        this.data[`${activeType}Page`]++
        await this.getData(activeType)
    },

    onTabChange(e) {
        this.setData({
            active: e.detail.name
            ,
        })
    },

    onPageScroll(e) {
        mixins.onPageScroll(e, this, 'isTop')
    },

    async onTapCard(e) {
        const {item, field} = e.currentTarget.dataset
        const res = await wx.navigateTo({
            url: `/pages/${field}-detail/${field}-detail`
        })
        res.eventChannel.emit('detail', item)
        res.eventChannel.on('vote', res => {
            const voteTargetIndex = this.data[`${res.type}List`].findIndex(item => item.id === res.id)
            this.setData({
                [`${res.type}List[${voteTargetIndex}].coin`]: res.coin
            })
        })
    },
});
